//
//  BSKDerectorySelecterView.m
//  MicroMemory
//
//  Created by rimi on 16/7/23.
//  Copyright © 2016年 BlueSky. All rights reserved.
//

#import "BSKDerectorySelecterView.h"
#import "BSKNote.h"
#import "BSKconsts.h"
#import "BSKTheme.h"


@interface BSKDerectorySelecterView ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,weak)NSArray * noteList;
@property(nonatomic,copy)void(^selectedCallBack)(NSInteger index);
@property(nonatomic,strong) UITableView * table;
@property(nonatomic,strong) UIView * ContentView;
@end

@implementation BSKDerectorySelecterView

- (instancetype)initWithNoteList:(NSArray *)array selectedCallBack:(void(^)(NSInteger index))callback
{
	self = [super init];
	if (self) {
		_noteList = array;
		_selectedCallBack = callback;
		[self loadUI];
	}
	return self;
}
-(void)loadUI{
	self.frame = [UIScreen mainScreen].bounds;
	UIView * bgView = [[UIView alloc]initWithFrame:self.bounds];
	self.backgroundColor = [UIColor clearColor];
	bgView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
	[self addSubview:bgView];
	
	_ContentView = [[UIView alloc]init];
	_ContentView.center = CGPointMake(ScreenWidth/2, ScreenHeight/2);
	_ContentView.backgroundColor = [UIColor whiteColor];
	
	CGFloat ContentHeight = 40*_noteList.count+50;
	if (ContentHeight > ScreenHeight * 0.7) {
		ContentHeight = ScreenHeight*0.7;
	}
	_ContentView.bounds = CGRectMake(0, 0, ScreenWidth*0.7,  ContentHeight);
	_ContentView.layer.masksToBounds = YES;
	_ContentView.layer.cornerRadius = 20;
	[self addSubview:_ContentView];
	_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, _ContentView.bounds.size.width, 50)];
	_titleLabel.textAlignment = NSTextAlignmentCenter;
	_titleLabel.textColor = [UIColor whiteColor];
	_titleLabel.backgroundColor = [BSKTheme curentTheme].noteBackgroundColor;
	[_ContentView addSubview:_titleLabel];
	
	self.alpha = 0;
	_table = [[UITableView alloc]init];
	_table.frame = CGRectMake(0, 50, _ContentView.bounds.size.width, _ContentView.bounds.size.height-50);
	_table.dataSource = self;
	_table.delegate = self;
	_table.rowHeight = 40;
	UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
	[bgView addGestureRecognizer:tapGesture];
	[self.ContentView addSubview:_table];
}

-(void)show{
	self.transform = CGAffineTransformMakeScale(1.5, 1.5);
	[UIView animateWithDuration:0.25 animations:^{
		self.transform = CGAffineTransformMakeScale(1, 1);
		self.alpha = 1;
	} completion:^(BOOL finished) {
		
	}];
}

-(void)hide{
	self.transform = CGAffineTransformMakeScale(1, 1);
	[UIView animateWithDuration:0.25 animations:^{
		self.transform = CGAffineTransformMakeScale(1.5, 1.5);
		self.alpha = 0;
	} completion:^(BOOL finished) {
		[self removeFromSuperview];
	}];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return _noteList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"typecell"];
	if (!cell) {
		cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"typecell"];
	}
	BSKNote * note = _noteList[_noteList.count - 1 -indexPath.row];
	cell.textLabel.text = note.title;
	cell.detailTextLabel.text = note.date;
	
	return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	if (_selectedCallBack != nil) {
		_selectedCallBack(indexPath.row);
	}
	[self hide];
}

@end
